package com.cloud.mall.controller;


import com.cloud.mall.entity.User;
import com.cloud.mall.response.R;
import com.cloud.mall.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 芋头同学
 * @since 2021-08-20
 */
@Api(tags = "用户操作接口")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService service;

    /**
     * 根据 Id 查询用户
     *
     * @param id 用户Id
     * @return 查询到的用户
     */
    @ApiImplicitParam(name = "id", value = "用户 Id")
    @ApiOperation("根据用户 Id 查询用户")
    @GetMapping("{id}")
    public R findUserById(@PathVariable String id) {

        User user = service.getById(id);

        return R.ok().data("user", user);
    }

    /**
     * 查询所有用户
     *
     * @return 用户集合
     */
    @ApiOperation("查询所有用户")
    @GetMapping("/all")
    public R findAllUser() {

        List<User> users = service.list();

        return R.ok().data("users", users);
    }

    /**
     * 用户登录功能
     *
     * @param account  用户账户
     * @param password 用户密码
     * @return 用户信息
     */
    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账户", required = true),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true)
    })
    @PostMapping("/login")
    public R userLogin(String account, String password) {

        User user = new User(account, password);
        user = service.login(user);

        return R.ok()
                .message(user == null ? "用户名或密码错误" : "")
                .data("user", user);
    }

    @PutMapping()
    public R createUser(@RequestBody User user) {
        return R.ok();
    }

}

